package com.mindmax.user.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.mindmax.user.domain.AdminUser;
import com.mindmax.user.service.IAdminUserService;
import com.mindmax.user.vo.BindingTenantUserVo;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 忆图用户Controller
 *
 * @author cabloy
 * @date 2024-04-02
 */
@RestController
@RequestMapping("/b/user")
@DataSource(value = DataSourceType.YT_ADMIN)
public class AdminUserController extends BaseController
{
    @Autowired
    private IAdminUserService adminUserService;

    /**
     * 为用户绑定租户
     */
    @PostMapping("/tenant")
    public void rebindingUserTenant(@RequestBody BindingTenantUserVo bindingTenantUserVo)
    {
        logger.info("绑定用户租户: {}", JSON.toJSONString(bindingTenantUserVo));
        adminUserService.rebindingUserTenant(bindingTenantUserVo);
    }

    /**
     * 查询忆图用户列表
     */
    @PreAuthorize("@ss.hasPermi('b:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdminUser adminUser)
    {
        startPage();
        List<AdminUser> list = adminUserService.selectAdminUserList(adminUser);
        return getDataTable(list);
    }

    /**
     * 导出忆图用户列表
     */
    @PreAuthorize("@ss.hasPermi('b:user:export')")
    @Log(title = "忆图用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdminUser adminUser)
    {
        List<AdminUser> list = adminUserService.selectAdminUserList(adminUser);
        ExcelUtil<AdminUser> util = new ExcelUtil<AdminUser>(AdminUser.class);
        util.exportExcel(response, list, "忆图用户数据");
    }

    /**
     * 获取忆图用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('b:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(adminUserService.selectAdminUserById(id));
    }

    /**
     * 新增忆图用户
     */
    @PreAuthorize("@ss.hasPermi('b:user:add')")
    @Log(title = "忆图用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdminUser adminUser)
    {
        return toAjax(adminUserService.insertAdminUser(adminUser));
    }

    /**
     * 修改忆图用户
     */
    @PreAuthorize("@ss.hasPermi('b:user:edit')")
    @Log(title = "忆图用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdminUser adminUser)
    {
        return toAjax(adminUserService.updateAdminUser(adminUser));
    }

    /**
     * 删除忆图用户
     */
    @PreAuthorize("@ss.hasPermi('b:user:remove')")
    @Log(title = "忆图用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(adminUserService.deleteAdminUserByIds(ids));
    }
}
